package com.unitd.base.module.consumer.comm.util.wechat;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;


/**
 * 与微信进行处理的MD5加密工具类
 *
 * @author Hudan
 * @version 1.0
 * @filename MD5.java
 * @copyright www.unitd.com
 * @date 2020-07-09
 */
public class MD5 {
    private MD5() {
    }

    /**
     * 生成 MD5
     *
     * @param data 待处理数据
     * @return 加密后的秘钥
     */
    public static String getMessageDigest(String data) {
        StringBuilder sb = new StringBuilder();
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] array = md.digest(data.getBytes(StandardCharsets.UTF_8));

            for (byte item : array) {
                sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
            }
        } catch (Exception e) {
            return null;
        }
        return sb.toString().toUpperCase();
    }
}